package com.duowei.diandan.util;

import android.os.Environment;
import android.widget.ImageView;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.duowei.diandan.R;
import com.duowei.diandan.app.App;
import com.duowei.diandan.app.GlideApp;
import com.duowei.diandan.bean.food.FoodImpl;
import com.duowei.diandan.bean.food.FoodItem;
import com.duowei.diandan.bean.food.Meal;
import com.duowei.diandan.bean.food.Single;
import com.duowei.diandan.bean.food.SingleAndMeals;
import com.duowei.diandan.bean.food.SingleGroup;
import com.duowei.diandan.db.entry.Tcsd;
import com.duowei.diandan.view.PriceTextView;

import java.io.File;

public class FoodHelper {

    /**
     * @param imageNo   图片编号
     * @param imageView ImageView
     */
    public static void setCoverImage(String imageNo, ImageView imageView) {
        File file = new File(Environment.getExternalStorageDirectory(),
                "/Dw/food_images/" + imageNo + ".png");

        if (file.exists()) {
            GlideApp.with(App.getApp())
                    .load(file)
                    .placeholder(R.drawable.ic_photograph_2)
                    .centerCrop()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .override(200, 200)
                    .into(imageView);
        } else {
            GlideApp.with(App.getApp())
                    .load(R.drawable.ic_photograph_2)
                    .centerCrop()
                    .into(imageView);
        }
    }

    /**
     * @param food          食物项
     * @param priceTextView PriceTextView
     */
    public static void setPrice(FoodImpl food, PriceTextView priceTextView) {
        if (food.getPrice() == null) {
            priceTextView.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, 0, 0);
            priceTextView.setText(null);

        } else {
            if (food instanceof FoodItem
                    || food instanceof Single
                    || food instanceof SingleAndMeals
                    || food instanceof SingleGroup
                    || food instanceof Meal
                    || food instanceof Tcsd) {

                if (food.getMemberPrice() != null) {
                    priceTextView.setCompoundDrawablesRelativeWithIntrinsicBounds(
                            0, 0, R.drawable.ic_member_price, 0);
                    priceTextView.setPrice("¥%s", food.getPrice());
                } else {
                    priceTextView.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, 0, 0);
                    priceTextView.setPrice(food.getPrice());
                }

            } else {
                priceTextView.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, 0, 0);
                priceTextView.setPrice(food.getPrice());
            }
        }
    }
}
